// var fcbuf = require("../fcbuf");
function rq(url, data, load, type, callback) {
  if (url != '/Login/searchCompany') {
    swan.showToast({
      title: '正在加载',
      icon: 'loading',
      mask: true
    });
  }
  return new Promise((resolve, reject) => {
    let baseU = swan.getStorageSync("url");
    baseU = baseU ? baseU : "https://bd.jsbestop.com/index.php/Api";
    let targeturl = baseU + url;
    let data1 = data ? data : {};
    let header = {};
    let method = type ? type : 'GET';
    data1['session_key'] = swan.getStorageSync("session_key");
    let session_id = swan.getStorageSync("session_id");
    header['cookie'] = "PHPSESSID=" + session_id;
    if (method.toLowerCase() == 'post') {
      header['Content-Type'] = 'application/x-www-form-urlencoded;charset=utf-8';
    }
    swan.request({
      url: targeturl,
      header: header,
      data: data1,
      method,
      success: function (res) {
        // if (load == "fcbuf") fcbuf.submit({ t: 3 });
        if (res.data.code == 200) {
          resolve(res.data);
          if(url == '/User/getfaxiandetail'){
            setTimeOut(function(){
              swan.hideToast();
            },1500);
          }else{
            swan.hideToast();
          }
          // swan.hideToast();
        } else if (res.data.code == 400) {
          //这里加上sessionkey过期自动登录逻辑
          getApp().checkSetting(callback);
        } else if (res.data.code == 401) {
          //如果没有绑定企业，或者绑定错误企业
          swan.hideToast();
          swan.showToast({
            title: '请绑定企业',
            icon: 'loading',
            mask: true,
            duration: 1000
          });
          setTimeout(function () {
            swan.navigateTo({ url: '../bindphone/bindphone' });
          }, 1000);
        } else {
          resolve(res.data);
        }
      },
      fail: function (res) {
        reject(res);
      }
    });
  });
}

//获取随机数
function gr(n) {
  var chars = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'];
  var res = "";
  for (var i = 0; i < n; i++) {
    var id = Math.ceil(Math.random() * 35);
    res += chars[id];
  }
  return res;
}

module.exports = {
  rq: rq,
  gr: gr
};